package com.waypolice.manager.controller;

import com.alibaba.fastjson.JSONArray;
import com.waypolice.base.BaseController;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.manager.service.CmsSinItemService;
import com.waypolice.manager.service.UpmsArchivesTypesService;
import com.waypolice.manager.service.YearArchiveHappenService;
import com.waypolice.pojo.CmsSinItem;
import com.waypolice.pojo.CmsSinItemExample;
import com.waypolice.pojo.UpmsArchivesTypes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 各年度归档情况统计
 * @Author wangrang
 * @Date Created in 9:01 2019/4/1
 * @Modified By:
 */
@Controller
@Api(value = "各年度归档情况统计", description = "档案统计之各年度归档情况统计")
@RequestMapping(value="/pcwindow")
public class YearArchiveHappenStatAction extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(YearArchiveHappenStatAction.class);

    @Autowired
    private CmsSinItemService cmsSinItemService;

    @Autowired
    private YearArchiveHappenService yearArchiveHappenService;

    @Autowired
    private UpmsArchivesTypesService upmsArchivesTypesService;

    @ApiOperation(value = "跳转至各年度归档情况统计页面")
    @RequiresPermissions("cms:year:count")
    @RequestMapping("/yearArchive")
    public String toVolItemManagement(){

        return "countManager/yearArchiveHappen";
    }

    @ApiOperation(value = "各年度归档数据统计")
    @RequestMapping(value = "/count/yearArchiverCount", method = RequestMethod.GET)
    @ResponseBody
    public Object yearArchiverCount(HttpServletRequest request,
                              @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                              @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                              String yearCountStart, String yearCountEnd) {
        List<Map<String, String>> objects = new ArrayList<>();
        if(StringUtils.isBlank(yearCountStart) || StringUtils.isBlank(yearCountEnd)){
            //获取年份和总案卷数,总件数
            String year = DateTimeUtil.getYear();
            Map<String, String> yearCountData = getYearCountData(year);
            objects.add(yearCountData);
        }else{
            Integer yearStart = Integer.valueOf(yearCountStart);
            Integer yearEnd = Integer.valueOf(yearCountEnd);
            for (int i = yearStart; i <= yearEnd; i++) {
                Map<String, String> yearCountData = getYearCountData(String.valueOf(i));
                objects.add(yearCountData);
            }
        }
        //最终的list集合,分页操作
        PageUtils<Map<String, String>> pageUtils = new PageUtils<Map<String, String>>();
        List<Map<String, String>> cItemPage = pageUtils.pageList(objects, offset, limit);
        Map<String, Object> result = new HashMap<>();
        result.put("rows",cItemPage);
        result.put("total",objects.size());
        return result;
    }

    private  Map<String, String> getYearCountData(String year) {
        String totalItemNum = yearArchiveHappenService.getTotalItemNum(year,null);
        String totalVolNum = yearArchiveHappenService.getTotalVolNum(year,null);
        Map<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("year",year);
        stringStringHashMap.put("totalVol",totalVolNum);
        stringStringHashMap.put("totalItem",totalItemNum);

        //获取各类型的案卷总数,案件总数
        List<UpmsArchivesTypes> allTypes = upmsArchivesTypesService.findAllTypes();
        for (UpmsArchivesTypes allType : allTypes) {
            String type = allType.getSortNum();
            String itemNumByType = yearArchiveHappenService.getTotalItemNum(year,type);
            String volNumByType = yearArchiveHappenService.getTotalVolNum(year,type);
            String volNum = "a"+type ;
            String itemAndSinNum ="b"+ type;
            stringStringHashMap.put(volNum,volNumByType);
            stringStringHashMap.put(itemAndSinNum,itemNumByType);
        }
        return stringStringHashMap;
    }

    @ApiOperation(value = "各年度归档数据统计")
    @RequestMapping(value = "/count/yearCountECharts", method = RequestMethod.POST)
    @ResponseBody
    public String yearCountECharts(HttpServletRequest request, String yearCountStart, String yearCountEnd) {
        if(StringUtils.isBlank(yearCountStart) || StringUtils.isBlank(yearCountEnd)){
            //获取年份和总案卷数,总件数
            String newYear = DateTimeUtil.getYear();
            Map<String, Object> newYearECharts = yearArchiveHappenService.getNewYearECharts(newYear);
            net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(newYearECharts);
            String string = jsonArray.toString();
            return string;
        }else{
            Map<String, Object> manyYearsECharts = yearArchiveHappenService.getManyYearsECharts(yearCountStart, yearCountEnd);
            net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(manyYearsECharts);
            String string = jsonArray.toString();

            return string;
        }

    }
}
